import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

/**
 * 重写路由的push方法
 */
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error=> error)
}

const routes = [
  {
    path:'',
    redirect: '/home'
  },
  {
    path:'/home',
    component:()=>import('../views/home/Home'),
    meta: { 
      title: '首页'
     }
  },
  {
    path:'/cart',
    component:()=>import('../views/cart/Cart'),
    meta: { 
      title: '购物车'
     }
  },
  {
    path:'/category',
    component:()=>import('../views/category/Category'),
    meta: { 
      title: '分类'
     }
  },
  {
    path:'/profile',
    component:()=>import('../views/profile/Profile'),
    meta: {
      title: '我的'
    }
  },
  {
    //使用动态路由传值  获取值：this.$route.params.id
    // path:'/detail/:id',
    path: '/detail',
    component:()=>import('../views/detail/Detail'),
    meta: {
      title: '详情'
    }
    //还可以使用query传值
    //this.$router.push({path,query:{}})
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // 进入一个route时触发
  document.title = to.meta.title;
  next();
  
})

export default router
